package cn.tedu.hongbei.pms.controller;

import cn.tedu.commons.web.JsonResult;
import cn.tedu.hongbei.pms.pojo.param.AttributeAddNewParam;
import cn.tedu.hongbei.pms.service.IAttributeService;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.hibernate.validator.constraints.Range;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;

@RestController
@RequestMapping("/attribute/")
@Api(tags = "4.属性管理")
@Slf4j
@Validated
public class AttributeController {
    @Autowired
    IAttributeService iAttributeService;

    @PostMapping("/add-new")
    @PreAuthorize("hasAuthority('/pms/product/add-new')")
    @ApiOperation("添加属性")
    @ApiOperationSupport(order = 100)
    public JsonResult<Void> addNew(@Valid AttributeAddNewParam attributeAddNewParam) {
        log.debug("开始处理【添加属性】的请求，参数：{}", attributeAddNewParam);
        iAttributeService.addNew(attributeAddNewParam);
        return JsonResult.ok();
    }

    @GetMapping("list")
    @ApiOperation("查询属性")//给此请求加上说明
    @ApiOperationSupport(order = 400)//order增加一个序号来方便排序
    @ApiImplicitParams({
            @ApiImplicitParam(name = "templateId", value = "属性模板ID", required = true, dataType = "long", example = "1"),
    })
    public JsonResult list(@Range(min = 1, message = "根据属性模板ID查找属性失败，请提交合法的属性模板ID值！") @RequestParam Long templateId){
        return JsonResult.ok(iAttributeService.list(templateId));
    }
}
